﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Client_Server_Interface;

namespace BTM_Client_Application
{
    /// <summary>
    /// Логика взаимодействия для DeviceInStationInfo.xaml
    /// </summary>
    public partial class DeviceInStationInfo : IDisposable
    {
        private readonly VoidDelegate _toDispose;
        private readonly IDeviceInterface _device;

        public DeviceInfoControl DeviceInfoControl { get { return DeviceInfoControl.GetControl(_device); }}

        private DeviceInStationInfo()
        {
            InitializeComponent();
        }

        public DeviceInStationInfo(IDeviceInterface device) : this()
        {
            _device = device;
            device.ErrorsListChanged += DeviceOnErrorsListChanged;
            _toDispose = () => { device.ErrorsListChanged -= DeviceOnErrorsListChanged; };
            ErrorsCountBox.Content = device.UnreadCount;
            DeviceNameBox.Content = device.DevInfo.Title;
        }

        private void DeviceOnErrorsListChanged(object sender, EventArgs eventArgs)
        {
            var dev = sender as IDeviceInterface;
            if (dev != null)
            {
                Dispatcher.Invoke(new VoidDelegate(() =>
                {
                    ErrorsCountBox.Content = dev.UnreadCount.ToString(CultureInfo.InvariantCulture);
                    AllErrorsCountBox.Text = dev.ErrorsList.Count.ToString(CultureInfo.InvariantCulture);
                }));
            }
                
        }

        public void Dispose()
        {
            _toDispose();
        }
    }
}
